package com.excel;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.SQLException;
import java.util.List;
import java.util.Locale;

import jxl.CellView;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.biff.EmptyCell;
import jxl.format.Alignment;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.read.biff.BiffException;
import jxl.write.Border;
import jxl.write.Label;
import jxl.write.NumberFormats;
import jxl.write.VerticalAlignment;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public abstract class MExcelWriter {

	WorkbookSettings ws = new WorkbookSettings();
	WritableWorkbook workbook = null;
	WritableSheet s;
	protected int dataRowCount = 0;
	/**
	 * 表格名
	 */
	protected String tableDisplayName;
	protected String sheetName;
	protected String fileName;
	/**
	 * 表格在Excel中的Y方向的坐标
	 */
	protected int tableYIndex = -1;
	/**
	 * 表格在Excel中的X方向的坐标
	 */
	protected int tableXIndex = -1;

	/**
	 * 表格之间的间隙行数
	 */
	public static int LINE_GAP = 2;

	/**
	 * 表头设置信息
	 * 
	 * @see MergedColumns
	 */
	// private String formatStr;
	public MExcelWriter(String fileName) {
		this.fileName = fileName;
	}



	public void init(OutputStream os) throws SQLException {
		ws.setLocale(new Locale("utf-8", "UTF-8"));
		try {		
				workbook = Workbook.createWorkbook(os, ws);				
				s = workbook.createSheet(sheetName, 0);	
		} catch (IOException e) {
			e.printStackTrace();
		}
		initConfig();
		
		if (this.tableXIndex == -1) {
			this.tableXIndex = 0;
		}

		if (this.tableYIndex == -1) {
			if (this.s.getRows() == 0) {
				this.tableYIndex = 1;
			} else {
				this.tableYIndex = this.s.getRows() + LINE_GAP;
			}

		}
	}

	public abstract void initConfig();

	public int getSheetIndex(WritableWorkbook workbook, String sheetName) {

		int index = 0;
		s = workbook.getSheet(sheetName);

		String names[] = workbook.getSheetNames();

		for (int i = 0; i < names.length; i++) {
			if (names[i] == sheetName) {
				index = i;
				break;
			}
		}
		return index;
	}

	public void setPosition(int x, int y) {
		this.tableXIndex = x;
		this.tableYIndex = y;
	}

	public void writeExcel() throws WriteException, IOException {
		
		// 初始化Excel

		// 1.0取得字段名
		// 1.1写表头数据
		writeTableName();

		// 1.2将数据导入excel
		writeDataSheet();

		

	}

	/**
	 * 必须在操作结束时调用
	 * 
	 * @throws IOException
	 * @throws WriteException
	 */
	public void exit() throws IOException, WriteException {
		workbook.write();
		workbook.close();
	}

	/**
	 * 表头 初始位置设在(1,0)
	 * 
	 * @param s
	 * @throws WriteException
	 * @throws SQLException
	 */
	protected void writeTableName() {
		tableYIndex = tableYIndex + 2;				
	}

	/**
	 * 起始位置从(2,0)开始
	 * 
	 * @param resultSet
	 * @throws WriteException
	 * @throws SQLException
	 */
	protected abstract void writeDataSheet();

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getTableDisplayName() {
		return tableDisplayName;
	}

	public void setTableDisplayName(String tableDisplayName) {
		this.tableDisplayName = tableDisplayName;
	}

	public String getSheetName() {
		return sheetName;
	}

	public void setSheetName(String sheetName) {
		this.sheetName = sheetName;
	}

}
